CREATE
OR REPLACE PROCEDURE zcrk_auto_to_fcrkhc () AS
v_date_str   VARCHAR2(8);  -- 日期部分 YYYYMMDD
    v_seq        NUMBER(4);    -- 流水号
    p_bill_no VARCHAR2(50);
    insert_phid NUMBER;
 BEGIN
    -- 获取当天日期字符串
    v_date_str := TO_CHAR(SYSDATE, 'YYYYMMDD');

    -- 查询当天的最大流水号
    SELECT NVL(MAX(TO_NUMBER(SUBSTR(bill_no, 9, 4))), 0) + 1
      INTO v_seq
      FROM p_form_fcrkhcd_m
     WHERE SUBSTR(bill_no, 1, 8) = v_date_str;

    -- 拼接编码 = 日期 + 流水号
    p_bill_no := v_date_str || LPAD(v_seq, 4, '0');

-- 直接 FOR 循环查询结果
    FOR rec IN (
        SELECT
            *
        FROM
            kc_billhead t
        WHERE -- 辅材
            t.user_cllx = '02' and phid_transno = '1' and NOT EXISTS (
            SELECT 1
            FROM kc_billhead t2
            WHERE t2.bill_no = t.bill_no and phid_transno = '1'
            GROUP BY t2.bill_no
            HAVING COUNT(*) > 1
        )
    ) LOOP
    -- 判断条件
    IF rec.tempflg = 2 THEN
            insert_phid :=seq_auto_phid_fcrkhc.nextval;
            -- TODO: 在这里写逻辑
            INSERT INTO
            p_form_fcrkhcd_m (
                phid,
                code,
                creator,
                editor,
                ng_insert_dt,
                ng_update_dt,
                ng_record_ver,
                cur_orgid,
                ng_phid_org,
                ng_phid_cu,--10
                ng_phid_bp,
                ng_phid_original,
                ng_orgid_original,
                ng_phid_ui_scheme,
                ng_sv_search_key,
                ng_sd_search_key,
                ng_share_sign,
                phid_schemeid,
                imp_info,
                ng_write_source,--20
                ng_data_status,
                arc_flag,
                asr_flag,
                printtime,
                printcount,
                phid_app,
                app_dt,
                app_status,
                wf_flag,
                phid_fill_psn, --30
                phid_org,
                phid_pc,
                bill_dt,
                bill_name,
                bill_no,
                u_gysmc, --36
                u_ck,--37 仓库
                u_dhlb,
                u_dcrq,
                u_ysrq, --40
                u_bhsjexx,
                u_bhsjedx,
                u_sl,
                u_htmc,
                u_htbm
            )
        VALUES
            (
                insert_phid,
                null,
                rec.creator, -- 人
                rec.creator, -- 编辑人
                SYSDATE,
                SYSDATE,
                1,
                1,
                1,
                1,--10
                0,
                0,
                0,
                0,
                '10',
                '1',
                5,
                null,
                null,
                0,-- 20
                0,
                0,
                0,
                null,
                null,
                null,
                null,
                0,
                0,
                rec.creator, --30 填充人
                rec.phid_org,
                rec.phid_pc,
                SYSDATE, -- 单据日期
                null,
                p_bill_no,  -- 单据编码
                rec.phid_supply_ent, --36
                rec.phid_warehouse,
                rec.user_dhlb,
                rec.user_dcrq,
                null, -- 40
                0 - COALESCE(rec.user_bhsjexx,0), -- -5 不含税金额（元）小写
                DECODE(rec.user_bhsjey, NULL, '零', '负' || rec.user_bhsjey), -- -4 不含税金额（元）大写
                null,
                null,
                rec.phid_contractno
            );
            -- 处理明细
            FOR rec_d IN (SELECT * FROM kc_billbody WHERE pphid = rec.phid) LOOP
                INSERT INTO
                    p_form_fcrkhcd_d1 (
                        phid,
                        code,
                        rel_key1,
                        asr_flag,
                        imp_info,
                        creator,
                        editor,
                        ng_insert_dt,
                        ng_update_dt,
                        ng_record_ver, --10
                        cur_orgid,
                        ng_phid_org,
                        ng_phid_cu,
                        ng_phid_bp,
                        ng_phid_original,
                        ng_orgid_original,
                        ng_phid_ui_scheme,
                        ng_sv_search_key,
                        ng_sd_search_key,
                        ng_share_sign, --20
                        ng_write_source,
                        ng_data_status,
                        lineid,
                        pphid,
                        phid_pc,
                        itemid,
                        res_propertys,
                        res_masterdata,
                        phid_resbs,
                        u_sl, --30
                        u_hsdj, --31
                        u_bhsdj,
                        u_hsje,
                        u_bhsje,
                        u_jhbm
                    )
                VALUES
                    (
                        seq_auto_phid_fcrkhc_detail.nextval,
                        null,
                        null,
                        0,
                        null,
                        null,--人
                        null, -- 人
                        SYSDATE,
                        SYSDATE,
                        1, -- 10
                        1,
                        1,
                        1,
                        0,
                        0,
                        0,
                        0,
                        '10',
                        '1',
                        5, --20
                        0,
                        0,
                        null,
                        insert_phid,
                        null,
                        rec_d.phid_itemid,
                        null,
                        null,
                        null,
                        0 - COALESCE(rec_d.qty,0), --30
                        rec_d.taxprc,
                        rec_d.prc,
                        0 - COALESCE(rec_d.tax_mony,0),
                        0 - COALESCE(rec_d.mony , 0),
                        rec_d.user_jhbm
                    );
            END LOOP;

            v_seq := v_seq + 1;
            p_bill_no := v_date_str || LPAD(v_seq, 4, '0');
        END IF;

    END LOOP;

commit;

EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('插入失败，错误信息：' || SQLERRM);

RAISE;

ROLLBACK;

END zcrk_auto_to_fcrkhc;